package com.xianyun.site.auth.service.impl;

import com.alibaba.cola.exception.BizException;
import com.xianyun.common.core.domain.TokenContent;
import com.xianyun.common.core.utils.TokenContentUtil;
import com.xianyun.common.redis.service.RedisService;
import com.xianyun.common.security.common.TokenUserProperties;
import com.xianyun.common.security.utils.JwtUtil;
import com.xianyun.site.iblog.api.user.dubbo.SystemUserEchoService;
import com.xianyun.site.auth.common.entity.SystemUser;
import com.xianyun.site.auth.service.ITokenResultService;
import com.xianyun.site.iblog.dto.user.LoginHistorySortQry;
import com.xianyun.site.iblog.dto.user.data.LoginHistoryDTO;
import com.xianyun.site.iblog.dto.user.data.SystemUserDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author mac_zyj
 */
@Slf4j
@Component
public class ITokenResultServiceImpl implements ITokenResultService {

    @Autowired
    private RedisService redisService;

    @Reference
    protected SystemUserEchoService systemUserEchoService;

    @SuppressWarnings("all")
    @Autowired
    private TokenUserProperties tokenUserProperties;

    @Override
    public Map<String, Object> getTokenByUserTokenResult(SystemUser loginUser, HttpServletRequest request) {
        SystemUserDTO loginUserUser = loginUser.getInnerUserInfo();
        TokenContent tokenContent=new TokenContent();
        tokenContent.setUid(loginUserUser.getId().longValue());
        tokenContent.setSessionId(request.getSession().getId());
        BeanUtils.copyProperties(loginUserUser,tokenContent,TokenContent.class);
        try {
            return initTokenResultMap(tokenContent,loginUserUser);
        }catch (Exception e){
            log.debug("userName:{},-- Token生成失败!",loginUserUser.getUserName());
            throw new BizException("Token生成失败!");
        }
    }

    private  Map<String,Object> initTokenResultMap(TokenContent tokenContent, SystemUserDTO systemUser) throws IOException {
        Map<String,Object> resultMap=new LinkedHashMap<>(3);
        String token= JwtUtil.sign(tokenContent,60 * 60 * 1000L,tokenUserProperties.getJwtSecretKey());
        redisService.setCacheObject(TokenContentUtil.getRedisSecurityKey(tokenContent.getSessionId()),token,1, TimeUnit.HOURS);
        resultMap.put("success",true);
        resultMap.put("token",token);
        resultMap.put("currentLogin",getLastLoginHistory(systemUser));
        return resultMap;
    }

    private List<LoginHistoryDTO> getLastLoginHistory(SystemUserDTO systemUser){
        LoginHistorySortQry qry= new LoginHistorySortQry();
        qry.setUserName(systemUser.getUserName());
        Map<String, Object> queryParams =new HashMap<>(1);
        queryParams.put("size",1);
        qry.setQueryParams(queryParams);
        return systemUserEchoService.selectLoginHistoryBySort(qry);
    }
}
